package com.example.diagnose.Service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.diagnose.Domain.DiagnoseRecord;
import com.example.diagnose.Mapper.DiagnoseRecordMapper;
import com.example.diagnose.Service.DiagnoseRecordService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DiagnoseRecordServiceImpl extends ServiceImpl<DiagnoseRecordMapper, DiagnoseRecord> implements DiagnoseRecordService {

    @Resource
    DiagnoseRecord diagnoseRecord;

    @Resource
    DiagnoseRecordMapper diagnoseRecordMapper;

    @Override
    public int addDiagnoseRecord(DiagnoseRecord diagnoseRecord) {
        return diagnoseRecordMapper.insert(diagnoseRecord);
    }

    @Override
    public List<DiagnoseRecord> getDiagnoseRecordByAccount(String account) {
        QueryWrapper<DiagnoseRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("user_account", account);
        wrapper.orderByDesc("time");
        return diagnoseRecordMapper.selectList(wrapper);
    }

    @Override
    public DiagnoseRecord getDiagnoseRecordByAccountLatest(String account) {
        List<DiagnoseRecord> records = getDiagnoseRecordByAccount(account);
        if(records.isEmpty()){
            return null;
        }else {
            return records.get(0);
        }
    }

    @Override
    public int updateDiagnoseRecord(DiagnoseRecord diagnoseRecord) {
        return diagnoseRecordMapper.updateById(diagnoseRecord);
    }
}
